package com.interest.controller;

import com.interest.controller.builder.DefaultModelAndViewBuilder;
import com.interest.controller.builder.ListModelAndViewBuilder;
import com.interest.modules.task.entity.Task;
import com.interest.modules.task.entity.TaskHelper;
import com.interest.modules.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @RequestMapping("/delete")
    public ModelAndView delete(int id) {
        int code = taskService.delete(id);
        return new DefaultModelAndViewBuilder<Integer>("result", code).build();
    }

    @RequestMapping("/add")
    public ModelAndView add(String task) {
        int code = taskService.add(TaskHelper.from(task));
        return new DefaultModelAndViewBuilder<Integer>("result", code).build();
    }

    @RequestMapping("/detail")
    public ModelAndView getDetail(int id) {
        Task task = taskService.getDetail(id);
        return new DefaultModelAndViewBuilder<Task>("task/detail", task).build();
    }

    @RequestMapping("/list")
    public ModelAndView getList() {
        List<Task> list = taskService.getList();
        return new ListModelAndViewBuilder("task/list", list).build();
    }


    @RequestMapping("/update")
    public ModelAndView update(String task) {
        int code = taskService.update(TaskHelper.from(task));
        return new DefaultModelAndViewBuilder<Integer>("result", code).build();
    }

    @RequestMapping("/complete")
    public ModelAndView complete(int id) {
        int code = taskService.complete(id);
        return new DefaultModelAndViewBuilder<Integer>("result", code).build();
    }

    @RequestMapping("/activate")
    public ModelAndView activate(int id) {
        int code = taskService.activate(id);
        return new DefaultModelAndViewBuilder<Integer>("result", code).build();
    }
}
